package com.gitee.aurora.core.rule.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gitee.aurora.core.rule.dao.RuleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class RuleService {

    @Autowired
    private RuleDao ruleDao;

    public Map<String, String> getRuleMap(JdbcTemplate jdbcTemplate, String tableName, String deptCode) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map = ruleDao.getRule(jdbcTemplate, tableName, deptCode);
        Object rule = map.get("RULE");
        return mapper.readValue(rule.toString(), Map.class);
    }

    public void saveRule(JdbcTemplate jdbcTemplate, String tableName, String deptCode, String ruleStr) {
        ruleDao.saveRule(jdbcTemplate, tableName, deptCode, ruleStr);
    }
}
